package cdp.tarasenko.epamevospring.model;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

/**
 * @author Oleksandr_Tarasenko
 */
@Document(collection = "geolocations")
public class CityGeoLocation {

    @Id
    private ObjectId id = new ObjectId();
    private String city;
    private String country;

    @Field(value = "pos")
    private Position pos;

    public CityGeoLocation(String city, String country, Position pos) {
        this.city = city;
        this.country = country;
        this.pos = pos;
    }

    public CityGeoLocation() {
    }

    public Position getPos() {
        return pos;
    }

    public void setPos(Position pos) {
        this.pos = pos;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    @Override
    public String toString() {
        String result = "";
        result += "{country:" + this.country + ", city:" + this.city
                + ", " + pos + "}";
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CityGeoLocation that = (CityGeoLocation) o;

        if (!city.equals(that.city)) return false;
        if (!country.equals(that.country)) return false;
        if (!pos.equals(that.pos)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = city.hashCode();
        result = 31 * result + country.hashCode();
        result = 31 * result + pos.hashCode();
        return result;
    }

    public static class Position {
        @Field(value = "ob")
        private double latitude;
        @Field(value = "pb")
        private double longitude;

        public Position(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public double getLatitude() {
            return latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Position position = (Position) o;

            if (Double.compare(position.latitude, latitude) != 0) return false;
            if (Double.compare(position.longitude, longitude) != 0) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result;
            long temp;
            temp = Double.doubleToLongBits(latitude);
            result = (int) (temp ^ (temp >>> 32));
            temp = Double.doubleToLongBits(longitude);
            result = 31 * result + (int) (temp ^ (temp >>> 32));
            return result;
        }

        @Override
        public String toString() {
            return "pos:{" +
                    "latitude=" + latitude +
                    ", longitude=" + longitude +
                    '}';
        }
    }
}
